﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSCAI.Common
{
    public static class CalculateHelper
    {
        public static DateTime ExpiredDate;
        public static string UserName = string.Empty;

        /// <summary>
        /// 产生序列号
        /// </summary>
        /// <param name="currentDate"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static string GenerateSn(DateTime currentDate, out string userName)
        {
            var year = currentDate.Year.ToString("0000").ToArray();
            var month = currentDate.Month.ToString("00").ToArray();
            var day = currentDate.Day.ToString("00").ToArray();
            var guid = Guid.NewGuid().ToString();
            var arry = guid.ToArray();

            arry[5] = year[3];
            arry[3] = year[2];
            arry[9] = year[1];
            arry[19] = year[0];

            arry[28] = month[0];
            arry[12] = month[1];

            arry[22] = day[0];
            arry[35] = day[1];
            userName = arry[15].ToString() + arry[3].ToString() + arry[31].ToString() + arry[17].ToString() + arry[29].ToString();

            return string.Join(string.Empty, arry);
        }

        public static DateTime GetExpriedDate(string sn)
        {
            try
            {
                var arry = sn.Replace("-", "").ToArray();
                var year = arry[16].ToString() + arry[8].ToString() + arry[3].ToString() + arry[5].ToString();
                var month = arry[24].ToString() + arry[11].ToString();
                var day = arry[19].ToString() + arry[31].ToString();
                var expriedDate = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));
                return expriedDate.AddDays(1);
            }
            catch (Exception e)
            {
                return DateTime.MinValue;
            }
        }

        public static string GetUserName(string sn)
        {
            try
            {
                var arry = sn.Replace("-", "").ToArray();
                return arry[13].ToString() + arry[3].ToString() + arry[27].ToString() + arry[15].ToString() + arry[25].ToString();
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }


        /// <summary>
        /// order=0 ,升序排序
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public static int[] QickSort(int[] arg, int order = 0)
        {
            int temp = 0;

            if (order == 1)
            {
                for (int i = 0; i < arg.Length - 1; i++)
                {
                    for (int j = i + 1; j < arg.Length; j++)
                    {
                        if (arg[i] > arg[j])
                        {
                            temp = arg[i];
                            arg[i] = arg[j];
                            arg[j] = temp;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < arg.Length - 1; i++)
                {
                    for (int j = i + 1; j < arg.Length; j++)
                    {
                        if (arg[i] < arg[j])
                        {
                            temp = arg[i];
                            arg[i] = arg[j];
                            arg[j] = temp;
                        }
                    }
                }
            }

            return arg;
        }



    }
}
